package com.zhonglz.service.impl;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhonglz.commons.base.BaseService;
import com.zhonglz.model.BuSeal;
import com.zhonglz.model.SysFileInfo;
import com.zhonglz.service.BuSealService;
import com.zhonglz.util.Constans;
import com.zhonglz.util.minio.MinioProperties;
import com.zhonglz.util.minio.MinioUtils;
import com.zhonglz.util.result.ResultUtils;
import com.zhonglz.util.seal.SealUtils;
import com.zhonglz.util.tools.UuidUtils;

/****
 * 印章接口服务实现
 * @author xiaozhong
 *
 */
@Service
@Transactional 
public class BuSealServiceImpl  extends BaseService implements BuSealService {

	/***
	 * 查询印章列表
	 */
	@Override
	public List<BuSeal> selectByDataList(Map<String, Object> mapParam) {
		List<BuSeal> list =buSealMapper.selectByDataList(mapParam);
		return list;
	}
	
	/***
	 * 根据id查询出印章信息
	 * @param id
	 * @return
	 */
	@Override
	public BuSeal selectById(String id) {
		return buSealMapper.selectByPrimaryKey(id);
	}
	
	/***
     * 生成临时模板印章
     * @param sealName 印章名称
     * @param sealType 印章类型
     */
	@Override
	public ResultUtils createTempSeal(String sealName, Integer sealType) {
		String filePaths;
        BufferedImage pic = null;
        if (sealType == 1) {
            // 生成第一种样式的方章
            pic = SealUtils.sealFanz01(sealName);
        } else if (sealType == 2) {
            // 生成第二种样式的方章
            pic = SealUtils.changfangx(sealName);
        } else if (sealType == 3) {
            // 生成第三种样式的方章
        	ResultUtils fanz = SealUtils.sealFanz03(100, sealName);
            if (fanz.getCode() != ResultUtils.CODE_SUCCESS) {
                return fanz;
            }
            pic =(BufferedImage) fanz.getObject();
        } else {
            pic = SealUtils.startChapterGraphics2D(sealName,"综合维修部");
        }
        String fileUid = UuidUtils.uuid();
        String fileSuffix = ".png";
        String fileRealName = fileUid +fileSuffix;
        String filePath = sysBaseSetService.baseSetValue(Constans.MINIO_PATH) + "/" + fileRealName;
        try {
        	ByteArrayOutputStream os = new ByteArrayOutputStream();
        	ImageIO.write(pic, "png", os);
        	InputStream input = new ByteArrayInputStream(os.toByteArray());
            MinioUtils.putObject(filePath, input);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章生成失败!");
        }
        ResultUtils resultUtils = sysFileInfoService.uploadFile(fileUid,fileRealName,filePath,fileSuffix);
        
		return new ResultUtils(ResultUtils.CODE_SUCCESS, "印章生成成功!", resultUtils.getObject());
	}

	@Override
	public ResultUtils createSeal(String sealFile, String sealName, String sealNumber,String sealType) {
		BuSeal buSeal = new BuSeal();
	 	String sealId = UuidUtils.uuid();
	 	buSeal.setSealId(sealId);
        // 印章名
        buSeal.setSealName(sealName);
        // 印章编号
        buSeal.setSealNumber(sealNumber);
        // 印章类型1模板印章2实体印章
        buSeal.setSealType(new Byte(sealType));
        // 印章文件
        buSeal.setSealFile(sealFile);
        // 创建时间
        buSeal.setCreateTime(new Date());
        if (buSealMapper.insertSelective(buSeal) > 0) {
            return new ResultUtils(ResultUtils.CODE_SUCCESS, "印章保存成功",sealId);
        } else {
            return new ResultUtils(ResultUtils.CODE_ERROR, "印章保存失败");
        }
	}

	

}
